home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / updates / update18.zoo / libg++ / src / xsmplhis.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-22  |  2.5 KB  |  105 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Dirk Grunwald (grunwald@cs.uiuc.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #ifdef __GNUG__
  19. #pragma implementation
  20. #endif
  21. #include <stream.h>
  22. #include <xsmplhis.h>
  23. #include <math.h>
  24.  
  25.  
  26. const int SampleHistogramMinimum = -2;
  27. const int SampleHistogramMaximum = -1;
  28.  
  29. SampleHistogram::SampleHistogram(double low, double high, double width)
  30. {
  31.     if (high < low) {
  32.     double t = high;
  33.     high = low;
  34.     low = t;
  35.     }
  36.  
  37.     if (width == -1) {
  38.     width = (high - low) / 10;
  39.     }
  40.  
  41.     howManyBuckets = int((high - low) / width) + 2;
  42.     bucketCount = new int[howManyBuckets];
  43.     bucketLimit = new double[howManyBuckets];
  44.     double lim = low;
  45.     for (int i = 0; i < howManyBuckets; i++) {
  46.     bucketCount[i] = 0;
  47.     bucketLimit[i] = lim;
  48.     lim += width;
  49.     }
  50.     bucketLimit[howManyBuckets-1] = HUGE;    /* from math.h */
  51. }
  52.  
  53. SampleHistogram::~SampleHistogram()
  54. {
  55.     if (howManyBuckets > 0) {
  56.     delete bucketCount;
  57.     delete bucketLimit;
  58.     }
  59. }
  60.  
  61. void
  62. SampleHistogram::operator+=(double value)
  63. {
  64.     int i;
  65.     for (i = 0; i < howManyBuckets; i++) {
  66.     if (value < bucketLimit[i]) break;
  67.     }
  68.     bucketCount[i]++;
  69.     this->SampleStatistic::operator+=(value);
  70. }
  71.  
  72. int
  73. SampleHistogram::similarSamples(double d)
  74. {
  75.     int i;
  76.     for (i = 0; i < howManyBuckets; i++) {
  77.     if (d < bucketLimit[i]) return(bucketCount[i]);
  78.     }
  79.     return(0);
  80. }
  81.  
  82. void
  83. SampleHistogram::printBuckets(ostream& s)
  84. {
  85.     for(int i = 0; i < howManyBuckets; i++) {
  86.     if (bucketLimit[i] >= HUGE) {
  87.         s << "< max : " << bucketCount[i] << "\n";
  88.     } else {
  89.         s << "< " << bucketLimit[i] << " : " << bucketCount[i] << "\n";
  90.     }
  91.     }
  92. }
  93.  
  94. void
  95. SampleHistogram::reset()
  96. {
  97.     this->SampleStatistic::reset();
  98.     if (howManyBuckets > 0) {
  99.     for (register int i = 0; i < howManyBuckets; i++) {
  100.         bucketCount[i] = 0;
  101.     }
  102.     }
  103. }
  104.  
  105.